package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.Profile;

public class ConstructPlayer extends JFrame {

	/**
	 * 
	 */
	//private static final long serialVersionUID = 1L;
	private String name;
	private String location;
	private Color color;
	private ImageIcon imageIcon;
	private JTextField nameText;
	private JTextField locationText;
	private JColorChooser tcc;
	private JLabel chosenColor;
	private JLabel pictureLabel;
	
	/**
	 * stand alone frame to create the player profile 
	 * sets the player name, location, color, and image
	 */
	public ConstructPlayer(){
		this.setTitle("Make Your Player");
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setMinimumSize(new Dimension(400,800));
		this.setMaximumSize(new Dimension(400,800));
		
		
		JLabel nameLabel = new JLabel("Name: ");
		pictureLabel = new JLabel("Picture: ");
		pictureLabel.setIcon(new ImageIcon("images/players/pickImage.png"));
		JLabel locationLabel = new JLabel("Location: ");
		
		
		nameText = new JTextField();
		nameText.addActionListener(new NameActionListener());
		nameText.setSize(100, 40);
		locationText = new JTextField(10);
		locationText.addActionListener(new LocationActionListener());
		locationText.setSize(100, 20);
		
		tcc = new JColorChooser(Color.RED);
        tcc.getSelectionModel().addChangeListener(new ColorChangeListener());
        tcc.setBorder(BorderFactory.createTitledBorder(
                                             "Choose Text Color"));
    	tcc.setPreviewPanel(new JPanel());
    	
    	chosenColor = new JLabel();
    	chosenColor.setForeground(Color.WHITE);
    	chosenColor.setText("#_- CHOSEN COLOR -_#  MAKE IT BRIGHT OR YOU WONT SEE SHIT!");
    	JPanel chosenCPanel = new JPanel();
    	chosenCPanel.add(chosenColor);
    	chosenCPanel.setBackground(Color.BLACK);
    	       
    	GridLayout layout = new GridLayout(0,2);
		layout.setVgap(10);
		JPanel info = new JPanel();
		info.setLayout(layout);
		info.add(nameLabel);
		info.add(nameText);
		info.add(locationLabel);
		info.add(locationText);
		
		GridLayout picLayout = new GridLayout(0,3);
		picLayout.setVgap(5);
		picLayout.setHgap(5);
		
		JPanel pictures = new JPanel();
		pictures.setLayout(picLayout);
		
	
		JButton image1 = new JButton();
		image1.setIcon(new ImageIcon("images/players/Homer.png"));
		image1.addActionListener(new ImageActionListener("images/players/Homer.png"));
	
		JButton image2 = new JButton();
		image2.setIcon(new ImageIcon("images/players/Jan.png"));
		image2.addActionListener(new ImageActionListener("images/players/Jan.png"));
	
		JButton image3 = new JButton();
		image3.setIcon(new ImageIcon("images/players/Snodgrass.png"));
		image3.addActionListener(new ImageActionListener("images/players/Snodgrass.png"));

		JButton image4 = new JButton();
		image4.setIcon(new ImageIcon("images/players/McCann.png"));
		image4.addActionListener(new ImageActionListener("images/players/McCann.png"));
	
		JButton image5 = new JButton();
		image5.setIcon(new ImageIcon("images/players/Wall-E.png"));
		image5.addActionListener(new ImageActionListener("images/players/Wall-E.png"));
		
		JButton image6 = new JButton();
		image6.setIcon(new ImageIcon("images/players/HAL.png"));
		image6.addActionListener(new ImageActionListener("images/players/HAL.png"));
		
		JButton image7 = new JButton();
		image7.setIcon(new ImageIcon("images/players/300.png"));
		image7.addActionListener(new ImageActionListener("images/players/300.png"));
		
		JButton image8 = new JButton();
		image8.setIcon(new ImageIcon("images/players/PaperClip.png"));
		image8.addActionListener(new ImageActionListener("images/players/PaperClip.png"));

		JButton image9 = new JButton();
		image9.setIcon(new ImageIcon("images/players/ChuckNorris.png"));
		image9.addActionListener(new ImageActionListener("images/players/ChuckNorris.png"));
		
		pictures.add(image1);
		pictures.add(image2);
		pictures.add(image3);
		pictures.add(image4);
		pictures.add(image5);
		pictures.add(image6);
		pictures.add(image7);
		pictures.add(image8);
		pictures.add(image9);
		
		JScrollPane picScroll = new JScrollPane(pictures);
		picScroll.setMaximumSize(new Dimension(450,250));
		picScroll.setPreferredSize(new Dimension(450,250));
		picScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		
		JButton done = new JButton();
		done.setText("Done");
		done.addActionListener(new DoneActionListener());
		
			
		this.setLayout(new FlowLayout());
		this.add(info);
		this.add(chosenCPanel);
		this.add(tcc);
		this.add(pictureLabel);
		this.add(picScroll);
		this.add(done);
		
		
		this.setMinimumSize(new Dimension(475,700));
		this.setPreferredSize(new Dimension(475,700));
		
		this.setVisible(true);
	}
	private class ColorChangeListener implements ChangeListener{
		@Override
	    public void stateChanged(ChangeEvent e) {
	        color = tcc.getColor();
	        chosenColor.setForeground(color);
	        System.out.println("Color changed");
	    }
	}
	private class DoneActionListener implements ActionListener{
	    public void actionPerformed(ActionEvent evt) {
	        if(name!= null){
	        	if(location != null){
	        		if(imageIcon != null){
	        				JOptionPane.showMessageDialog(null, "Ok your Done!");
	        				Profile profile = new Profile(name, color, imageIcon);
	        				try {
	        					ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("profile.dat"));
	        					oos.writeObject(profile);
	        					System.out.println("I wrote");
	        				} catch (FileNotFoundException fe) {
	        					fe.printStackTrace();
	        				} catch (IOException ie) {
	        					ie.printStackTrace();
	        				}
	        				System.exit(DISPOSE_ON_CLOSE);
	        		}
	    	        JOptionPane.showMessageDialog(null, "NO IMAGE!!");
	    	        return;
	        	}
		        JOptionPane.showMessageDialog(null, "NO LOCATION!");
		        return;
	        }
	        JOptionPane.showMessageDialog(null, "NO NAME!");
	        return;
	    }
	}
	private class ImageActionListener implements ActionListener{
	    private String image;
		public ImageActionListener(String img){
			image = img;
		}
		public void actionPerformed(ActionEvent evt) {
	       imageIcon = new ImageIcon(image);
	       pictureLabel.setIcon(new ImageIcon(image));
	    }
	}
	private class NameActionListener implements ActionListener{
	    public void actionPerformed(ActionEvent evt) {
	        name = nameText.getText();
	        System.out.println(name);
	    }
	}
	private class LocationActionListener implements ActionListener{
	    public void actionPerformed(ActionEvent evt) {
	        location = locationText.getText();
	        System.out.println(location);
	    }
	}
	
	public static void main(String[] args){
		new ConstructPlayer();
	}
}
